import { Inject, Injectable } from '@angular/core';
import { RES_URL } from '../app.component';
import { HttpService } from './http.service';
import { MenuQueryParam } from '../models/params/menu-query.param';
import { Observable } from 'rxjs';
import { ApiResult } from '../models/api.result';
import { MenuDto } from '../models/sys/menu.dto';
import { URLConst } from '../models/consts/url-const';
import { UserInfoDto } from '../models/sys/user-info.dto';

@Injectable({
  providedIn: 'root'
})
export class ApiService {

  constructor(private httpService: HttpService,
    @Inject(RES_URL) private baseUrl: string) { }

  // Menu
  getMenuTree(param?: MenuQueryParam): Observable<ApiResult<MenuDto[]>> {
    const url = this.baseUrl + URLConst.MENU_TREE;
    if (param == null) {
      return this.httpService.get<ApiResult<MenuDto[]>>(url);
    } else {
      console.log(`ready to get menu tree : ${url}`);
      return this.httpService.getWithParam<ApiResult<MenuDto[]>>(url, param);
    }
  }

  // User
  getUserInfo(): Observable<ApiResult<UserInfoDto>> {
    const url = this.baseUrl + URLConst.USER_INFO;
    return this.httpService.get(url);
  }

  // Test
  getCommonMessage(): Observable<ApiResult<string>> {
    const url = this.baseUrl + URLConst.COMMON_ROLE;
    return this.httpService.get(url);
  }

  getAdminMessage(): Observable<ApiResult<string>> {
    const url = this.baseUrl + URLConst.ADMIN_ROLE;
    return this.httpService.get(url);
  }

  getUserListPerm(): Observable<ApiResult<string>> {
    const url = this.baseUrl + URLConst.USER_LIST_PERM;
    return this.httpService.get(url);
  }

  getAllPerms(): Observable<ApiResult<string>> {
    const url = this.baseUrl + URLConst.ALL_PERMS;
    return this.httpService.get(url);
  }

  getOpenidPerm(): Observable<ApiResult<string>> {
    const url = this.baseUrl + URLConst.OPENID_PERM;
    return this.httpService.get(url);
  }
}
